package com.mb.mp.controller;

import com.alibaba.fastjson.JSONObject;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import com.mb.mp.domain.MpMemberCommissionDtlDO;
import com.mb.mp.domain.MpMemberInfoDO;
import com.mb.mp.domain.MpUserInfoDO;
import com.mb.mp.service.IMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王源
 * @program mp-parent
 * @description 品牌库未完成
 * @date 2018-09-04 16:44
 **/
@Controller
@RequestMapping("/mp/member")
public class MemberController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(MemberController.class);

    @Autowired
    private IMemberService memberService;

    @GetMapping("/index")
    String index(){
        return "/mp/member_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<MpMemberInfoDO> list = memberService.queryList(query);
        int total = memberService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/userIndex/{memberId}")
    String userIndex(@PathVariable("memberId") Long memberId, Model model){
        model.addAttribute("memberId",memberId);
        return "/mp/user_list";
    }

    /**
     * 用户列表
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/userList")
    public PageUtils userList(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<MpUserInfoDO> list = memberService.queryUserList(query);
        int total = memberService.userCount(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }


    @ResponseBody
    @PostMapping("/gain")
    public R gain(MpUserInfoDO userInfoDO) {
        int result = memberService.gain(userInfoDO);
        if(result != 0){
            return R.ok();
        }else{
            return R.error(1,"佣金操作失败，请联系管理员");
        }
    }

    @ResponseBody
    @PostMapping("/cancel")
    public R cancel(MpUserInfoDO userInfoDO) {
        int result = memberService.cancel(userInfoDO);
        if(result != 0){
            return R.ok();
        }else{
            return R.error(1,"佣金撤销操作失败，请联系管理员");
        }
    }


    @GetMapping("/commissionIndex/{memberId}")
    String commissionIndex(@PathVariable("memberId") Long memberId, Model model){
        model.addAttribute("memberId",memberId);
        return "/mp/member_commission";
    }

    @ResponseBody
    @GetMapping("/getCommission")
    public R getCommission(MpMemberCommissionDtlDO memberCommissionDtlDO){
        BigDecimal commission = memberService.getCommission(memberCommissionDtlDO.getMemberId(),memberCommissionDtlDO.getYearMonth());
        Map<String,Object> map = new HashMap<>();
        map.put("commission",commission);
        return R.ok(map);
    }


    @ResponseBody
    @GetMapping("/monthCommission")
    public String monthCommission(MpMemberCommissionDtlDO memberCommissionDtlDO){
        JSONObject resp = memberService.commissionStats(memberCommissionDtlDO);
        return resp.toJSONString();
    }

    @GetMapping("/getMember/{memberId}")
    String getMember(@PathVariable("memberId") Long memberId, Model model){
        model.addAttribute("memberInfo",memberService.getMember(memberId));
        return "/mp/member_edit";
    }

    @ResponseBody
    @PostMapping("/updateAliapyAccount")
    public R updateAliapyAccount(MpMemberInfoDO memberInfo, HttpServletRequest request){
        int result = memberService.updateAliapyAccount(memberInfo);
        return R.ok();
    }


    @GetMapping("/toSetVip/{memberId}")
    String toSetVip(@PathVariable("memberId") Long memberId, Model model){
        model.addAttribute("memberInfo",memberService.getMember(memberId));
        return "/mp/member_set_vip";
    }

    @ResponseBody
    @PostMapping("/setVip")
    public R setVip(MpMemberInfoDO memberInfoDO) {
        int result = memberService.setVip(memberInfoDO);
        if(result != 0){
            return R.ok();
        }else{
            return R.error(1,"升级VIP失败，请联系管理员");
        }
    }

    /**
     * 取消VIP
     * @param memberInfoDO
     * @return
     */
    @ResponseBody
    @PostMapping("/cancelVip")
    public R cancelVip(MpMemberInfoDO memberInfoDO) {
        int result = memberService.cancelVip(memberInfoDO);
        if(result != 0){
            return R.ok();
        }else{
            return R.error(1,"取消VIP失败，请联系管理员");
        }
    }

    /**
     * 删除会员
     * @param memberInfoDO
     * @return
     */
    @ResponseBody
    @PostMapping("/removeMember")
    public R  removeMember(MpMemberInfoDO memberInfoDO){
        int result = memberService.removeMember(memberInfoDO);
        if(result != 0){
            return R.ok();
        }else{
            return R.error(1,"删除会员失败，请联系管理员");
        }
    }


}
